package com.doradance.app.appactivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.doradance.R;
import com.doradance.account.UrlCenter;
import com.doradance.account.utils.ShareUtil;
import com.doradance.app.MainActivity;
import com.doradance.common.http.MyHttpRequest;
import com.doradance.entity.WexinBean;
import com.pachong.android.baseuicomponent.CompState;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.utils.EasyToast;
import com.pachong.android.frameworkbase.utils.io.SharePreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

import static com.doradance.account.utils.ShareUtil.getBabyId;

public class SelectBabyActivity extends BaseActivity {

    @Bind(R.id.id_myList)
    ListView mIdMyList;
//    List<WexinBean.BabiesBean> babies;
    ArrayList<WexinBean.BabiesBean> babyArrayList;
    MyAdapter myAdapter;
    private String babyId =null;
    private int selectPosition = -1;//用于记录用户选择的变量
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(!TextUtils.isEmpty(getBabyId())){
            MainActivity.start(this);
            return;
        }
        initToolsBar();
        initData();
    }

    private void initData() {
        MyHttpRequest request = new MyHttpRequest(SelectBabyActivity.this);
        request.get(UrlCenter.BABIES_ITEM, null, new DataRequestListener<List<WexinBean.BabiesBean>>(WexinBean.BabiesBean.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }
            @Override
            public void onSuccess(List<WexinBean.BabiesBean> babies) {
                super.onSuccess(babies);
                initAdapter(babies);

            }
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    private void initAdapter(List<WexinBean.BabiesBean> babies) {
            babyArrayList = new ArrayList<>();
            for (int i = 0; i < babies.size(); i++)
                babyArrayList.add(babies.get(i));
            myAdapter = new MyAdapter(this, babyArrayList);
            mIdMyList.setAdapter(myAdapter);
            mIdMyList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //获取选中的参数
                    selectPosition = position;
                    myAdapter.notifyDataSetChanged();
                    babyId=babyArrayList.get(position).getId();
                    ShareUtil.setBabyName(babyArrayList.get(position).getNick_name());
                }
            });

    }

    private void initToolsBar() {
        getCustomToolbar().addLeftImageButton(R.mipmap.back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getCustomToolbar().setTitle(getString(R.string.select_baby));
    }

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, SelectBabyActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_select_baby, null);
    }

    @OnClick(R.id.btn_select_baby)
    public void onViewClicked() {
        if(TextUtils.isEmpty(babyId)){
            EasyToast.showToast(getApplicationContext(),"请选择宝宝");
        }else{
            //选择的宝宝保存在本地
            ShareUtil.setBabyId(babyId);
            MainActivity.start(this);
        }

    }

    public class MyAdapter extends BaseAdapter {
        Context context;
        List<WexinBean.BabiesBean> brandsList;
        LayoutInflater mInflater;

        public MyAdapter(Context context, List<WexinBean.BabiesBean> mList) {
            this.context = context;
            this.brandsList = mList;
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return brandsList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @SuppressLint("NewApi")
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.adapter_item, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.ivHeadItem = (CircleImageView) convertView.findViewById(R.id.iv_head_item);
                viewHolder.tvNameItem = (TextView) convertView.findViewById(R.id.tv_name_item);
                viewHolder.ivSexItem = (ImageView) convertView.findViewById(R.id.iv_sex_item);
                viewHolder.tvBirthdayItem = (TextView) convertView.findViewById(R.id.tv_birthday_item);
                viewHolder.tvIntrodItem = (TextView) convertView.findViewById(R.id.tv_introd_item);
                viewHolder.rlAll = (RelativeLayout)convertView.findViewById(R.id.rl_all);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            Glide.with(getApplicationContext()).load(brandsList.get(position).getAvatar()).into(viewHolder.ivHeadItem);
            viewHolder.tvNameItem.setText(brandsList.get(position).getNick_name());
            if(brandsList.get(position).getGender().equals("1")){
                viewHolder.ivSexItem.setImageResource(R.mipmap.boy_light);
            }else{
                viewHolder.ivSexItem.setImageResource(R.mipmap.girl_light);
            }
            viewHolder.tvBirthdayItem.setText(brandsList.get(position).getAge_label());
            viewHolder.tvIntrodItem.setText(brandsList.get(position).getSign());
            if(selectPosition == position){
                viewHolder.tvNameItem.setTextColor(Color.parseColor("#ffffff"));
                viewHolder.rlAll.setBackground(getDrawable(R.drawable.shape_gradient_item));
                viewHolder.tvBirthdayItem.setTextColor(Color.parseColor("#ffffff"));
                viewHolder.tvIntrodItem.setTextColor(Color.parseColor("#ffffff"));
                if(brandsList.get(position).getGender().equals("1")){
                    viewHolder.ivSexItem.setImageResource(R.mipmap.boy_unlit);
                }else{
                    viewHolder.ivSexItem.setImageResource(R.mipmap.girl_unlit);
                }
            }else{
                viewHolder.tvNameItem.setTextColor(Color.parseColor("#B0B0B0"));
                viewHolder.rlAll.setBackground(getDrawable(R.drawable.select_item));
                viewHolder.tvBirthdayItem.setTextColor(Color.parseColor("#B0B0B0"));
                viewHolder.tvIntrodItem.setTextColor(Color.parseColor("#B0B0B0"));
                if(brandsList.get(position).getGender().equals("1")){
                    viewHolder.ivSexItem.setImageResource(R.mipmap.boy_light);
                }else{
                    viewHolder.ivSexItem.setImageResource(R.mipmap.girl_light);
                }
            }

            return convertView;
        }
    }

    public class ViewHolder {
        CircleImageView ivHeadItem;
        TextView tvNameItem;
        ImageView ivSexItem;
        TextView tvBirthdayItem;
        TextView tvIntrodItem;
        RelativeLayout rlAll;
    }
}
